<!-- 页面中的内容 -->

<!-- 列表 -->
<div class="list-div" id="listDiv">
<form action="{:url('addProduct')}" method="POST">
	<table cellpadding="3" cellspacing="1">
    	<tr>
    		{volist name="attrs" id="attr"}
    			<th>{$attr.attr_name}</th>
    		{/volist}
            <th width="80">编号</th>
            <th width="80">价格</th>
            <th width="80">库存</th>
			<th width="30">操作</th>
        </tr>
        {if condition="$products"}
	        {volist name="products" key="k" id="product"}
		        <tr>
					{volist name="attrs" key="k2" id="attr"}
			    		<td>
				    		<select name="product[{$k-1}][attr_value][]">
				    			<option value="">请选择</option>
				    			{volist name="attr['attr_value']" id="value"}
									
					    			<option value="{$key}" {if condition=" isset($product['attr_value'][$key]) && $product['attr_value'][$key] == $value"}selected="selected"{/if} >{$value}</option>

				    			{/volist}
				    		</select>
				    		<input type="hidden" name="product[{$k-1}][attr_id][]" value="{$k2}" />
				    	</td>
			    	{/volist}
			    	<td>
			    		<input type="text" size="8" name="product[{$k-1}][goods_code]" value='{if condition="$product['product_code']"}{$product.product_code}{else /}0{/if}' />
			    	</td>
			    	<td>
			    		<input type="text" size="8" name="product[{$k-1}][goods_price]" value='{if condition="$product['product_price']"}{$product.product_price}{else /}0.00{/if}' />
			    	</td>
			    	<td>
			    		<input type="text" size="8" name="product[{$k-1}][goods_number]" value='{if condition="$product['product_sku']"}{$product.product_sku}{else /}0{/if}' />
			    		<input type="hidden" name = "goods_id" value="{:input('id',0,'intval')}">
			    	</td>
					<td>
						<input type="button" onclick="addnew(this);" value='{if condition="$k==1"}+{else /}-{/if}' />
					</td>
			    </tr>
			{/volist}
		{else /}
			<tr>
				{volist name="attrs" id="attr"}
					
		    		<td>
			    		<select name="product[0][attr_value][]">
			    			<option value="">请选择</option>
			    			{volist name="attr['attr_value']" id="value"}				    				<option value="{$key}" >{$value}</option>
			    			{/volist}
			    		</select>
			    	</td>
		    	{/volist}
		    	<td>
		    		<input type="text" size="8" name="product[0][goods_code]" value='0' />
		    	</td>
		    	<td>
		    		<input type="text" size="8" name="product[0][goods_price]" value='0' />
		    	</td>
		    	<td>
		    		<input type="text" size="8" name="product[0][goods_number]" value='0' />
		    		<input type="hidden" name = "goods_id" value="{:input('id',0,'intval')}">
		    	</td>
				<td>
					<input type="button" onclick="addnew(this);" value='+' />
				</td>
		    </tr>
		{/if}
	        <tr id="btn">
	        	<td colspan="{$count+4}" align="center">
	        		<input type="submit" value="提交货品" />
	        	</td>
	        </tr>
	</table>
</form>
</div>
<script>

var key = $('tr').size()-2;

function addnew(btn){
	// 先获取点击的按钮所在的tr
	var tr = $(btn).parent().parent();
	if($(btn).val() == "+"){
		// 克隆tr
		var newtr = tr.clone();

		// 把+变-
		newtr.find(":button").val("-");

		//改变select的索引值
		var oSelect = newtr.find("select");
		var selectName = oSelect.attr('name');
		selectName=selectName.replace(/\d+/,key);
		oSelect.attr('name',selectName);

		//改变input的索引值
		var oInput = newtr.find('input[type=text],input[type=hidden]');
		var inputName = '';
		oInput.each(function() {

			inputName = $(this).attr('name');
			inputName = inputName.replace(/\d+/,key);
			$(this).attr('name',inputName);	

		});

		key++;	

		// 把到btn所在的TR前面
		$("#btn").before(newtr);
	}
	else
		tr.remove();

}
</script>